home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / glshade.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  213 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    glshade -
  19.  *        Simple support for describing materials and light sources 
  20.  *    This makes it easy for people without PHD's in GL to actually 
  21.  *    shade surfaces.
  22.  *
  23.  *                Paul Haeberli - 1990
  24.  *
  25.  *  exports:
  26.  *        matrixinit();
  27.  *        shadeinit();
  28.  *        shadeon();
  29.  *        shadeoff();
  30.  *        setdiffuse(r,g,b);
  31.  *        setspecular(r,g,b);
  32.  *        setshininess(s);
  33.  *
  34.  *
  35.  *  Here is the structure of a typical program:
  36.  *
  37.  *        main() 
  38.  *        {
  39.  *        winopen("shade");
  40.  *        RGBmode();
  41.  *        doublebuffer();
  42.  *        gconfig();
  43.  *        winopen("blat");
  44.  *        matrixinit();            you must use two matrix model 
  45.  *        .
  46.  *        .
  47.  *        perspective(  . . . . );
  48.  *        shadeinit();            init shading after perspective 
  49.  *        .
  50.  *        .
  51.  *        while(1)
  52.  *            cpack(0x00404060);        clear the screen
  53.  *            clear();
  54.  *            zclear();
  55.  *            .
  56.  *            .
  57.  *            shadeon();            turn lighting on
  58.  *                setdiffuse(1.0,0.0,0.0);    make the diffuse color red
  59.  *            drawobjwithnormals();    draw the object
  60.  *            shadeoff();            turn lighting off
  61.  *            .
  62.  *            .
  63.  *            swapbuffers();        swap buffers
  64.  *         }
  65.  *        }
  66.  */
  67. #include "stdio.h"
  68. #include "gl.h"
  69.  
  70. static initmaterial();
  71. static initlights();
  72.  
  73. static float   idmat[4][4] = {
  74.     1.0, 0.0, 0.0, 0.0,
  75.     0.0, 1.0, 0.0, 0.0,
  76.     0.0, 0.0, 1.0, 0.0,
  77.     0.0, 0.0, 0.0, 1.0
  78. };
  79.  
  80. matrixinit()
  81. {
  82.     mmode(MVIEWING); 
  83.     loadmatrix(idmat);
  84. }
  85.  
  86. shadeinit()
  87. {
  88.     initmaterial();
  89.     initlights();
  90.     shadeoff();
  91. }
  92.  
  93. shadeon()
  94. {
  95.     lmbind(LMODEL,1);
  96. }
  97.  
  98. shadeoff()
  99. {
  100.     lmbind(LMODEL,0);
  101. }
  102.  
  103. setdiffuse(r,g,b)
  104. float r, g, b;
  105. {
  106.     float c[3];
  107.  
  108.     lmcolor(LMC_DIFFUSE);
  109.     c[0] = r;
  110.     c[1] = g;
  111.     c[2] = b;
  112.     c3f(c);
  113.     lmcolor(LMC_COLOR);
  114. }
  115.  
  116. setspecular(r,g,b)
  117. float r, g, b;
  118. {
  119.     float c[3];
  120.  
  121.     lmcolor(LMC_SPECULAR);
  122.     c[0] = r;
  123.     c[1] = g;
  124.     c[2] = b;
  125.     c3f(c);
  126.     lmcolor(LMC_COLOR);
  127. }
  128.  
  129. setshininess(s)
  130. float s;
  131. {
  132.     float mat_desc[3];
  133.  
  134.     mat_desc[0] = SHININESS;
  135.     mat_desc[1] = s;
  136.     mat_desc[2] = LMNULL;
  137.     lmdef(DEFMATERIAL,1,3,mat_desc);
  138. }
  139.  
  140.  
  141. static initmaterial()
  142. {
  143.     float mat_desc[19];
  144.  
  145.     mat_desc[0] = EMISSION;
  146.     mat_desc[1] = 0.0;
  147.     mat_desc[2] = 0.0;
  148.     mat_desc[3] = 0.0;
  149.     mat_desc[4] = AMBIENT;
  150.     mat_desc[5] = 0.0;
  151.     mat_desc[6] = 0.0;
  152.     mat_desc[7] = 0.0;
  153.     mat_desc[8] = DIFFUSE;
  154.     mat_desc[9] = 0.7;
  155.     mat_desc[10] = 0.7;
  156.     mat_desc[11] = 0.7;
  157.     mat_desc[12] = SPECULAR;
  158.     mat_desc[13] = 0.9;
  159.     mat_desc[14] = 0.9;
  160.     mat_desc[15] = 0.9;
  161.     mat_desc[16] = SHININESS;
  162.     mat_desc[17] = 40.0;
  163.     mat_desc[18] = LMNULL;
  164.     lmdef(DEFMATERIAL,1,19,mat_desc);
  165.     lmbind(MATERIAL,1);
  166. }
  167.  
  168. static initlights()
  169. {
  170.     float li_desc[10];
  171.  
  172.     li_desc[0] = AMBIENT;
  173.     li_desc[1] = 0.10;
  174.     li_desc[2] = 0.10;
  175.     li_desc[3] = 0.10;
  176.     li_desc[4] = LOCALVIEWER;
  177.     li_desc[5] = 0.0;
  178.     li_desc[6] = ATTENUATION;
  179.     li_desc[7] = 1.0;
  180.     li_desc[8] = 0.0;
  181.     li_desc[9] = LMNULL;
  182.     lmdef(DEFLMODEL,1,10,li_desc);
  183.     lmbind(LMODEL,1);
  184.  
  185.     makelight(0,0.0,0.0,10.0);
  186.     makelight(1,-5.0,5.0,10.0);
  187.     makelight(2, 5.0,5.0,10.0);
  188. }
  189.  
  190. makelight(i,x,y,z)
  191. int i;
  192. float x, y, z;
  193. {
  194.     float li_desc[14];
  195.  
  196.     li_desc[0] = AMBIENT; 
  197.     li_desc[1] = 0.1; 
  198.     li_desc[2] = 0.1;
  199.     li_desc[3] = 0.1;
  200.     li_desc[4] = LCOLOR;
  201.     li_desc[5] = 0.5;
  202.     li_desc[6] = 0.5;
  203.     li_desc[7] = 0.5;
  204.     li_desc[8] = POSITION;
  205.     li_desc[9] = x;
  206.     li_desc[10] = y;
  207.     li_desc[11] = z;
  208.     li_desc[12] = 0.0;        /* zero means infinte light */
  209.     li_desc[13] = LMNULL;
  210.     lmdef(DEFLIGHT,i+1,14,li_desc);
  211.     lmbind(LIGHT0+i,i+1);
  212. }
  213.